const app = getApp();

import {
  post
} from '../../utils/request';
import {
  bMapTransqqMap
} from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    config: null,
    mchList: [],
  },

  // 获取列表
  getList() {
    let options = app.getPageOptions();
    let location = wx.getStorageSync('location') || {};
    wx.showLoading({
      title: '正在加载',
    });
    post(`/mch/info/list`, {
        ...location,
        inCouponId: options.inCouponId ? options.inCouponId : 0,
        tenantCode: app.globalData.tenantCode,
      })
      .then(resp => {
        wx.hideLoading();
        if (resp.code === 0) {
          this.setData({
            mchList: resp.data,
          });
        }
      });
  },

  // 商家导肮
  onAddress(e) {
    let {
      lng,
      lat,
      address
    } = e.currentTarget.dataset;
    let local = bMapTransqqMap(lng, lat);
    wx.openLocation({
      longitude: local.longitude,
      latitude: local.latitude,
      address: address,
    });
  },

  // 联系商家
  onServiceTel(e) {
    let {
      servicetel
    } = e.currentTarget.dataset;
    if (servicetel) {
      wx.makePhoneCall({
        phoneNumber: servicetel,
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList();
    app.getConfig(config => {
      this.setData({
        config
      });
    });
  },

});